<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">


<link rel="stylesheet" href="/myblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/myblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gao-qianwei.gitee.io","root":"/myblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一、元组简介    tuple是一个固定大小的不同类型值的集合，是泛化的std::pair。我们也可以把他当做一个通用的结构体来用，不需要创建结构体又获取结构体的特征，在某些情况下可以取代结构体使程序更简洁，直观。std::tuple理论上可以有无数个任意类型的成员变量，而std::pair只能是2个成员，因此在需要保存3个及以上的数据时就需要使用tuple元组了。">
<meta property="og:type" content="article">
<meta property="og:title" content="STL_tuple容器">
<meta property="og:url" content="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Atuple%E5%AE%B9%E5%99%A8/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="一、元组简介    tuple是一个固定大小的不同类型值的集合，是泛化的std::pair。我们也可以把他当做一个通用的结构体来用，不需要创建结构体又获取结构体的特征，在某些情况下可以取代结构体使程序更简洁，直观。std::tuple理论上可以有无数个任意类型的成员变量，而std::pair只能是2个成员，因此在需要保存3个及以上的数据时就需要使用tuple元组了。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-07T13:33:44.695Z">
<meta property="article:modified_time" content="2021-05-07T13:54:25.864Z">
<meta property="article:author" content="高乾威">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="STL">
<meta property="article:tag" content="STL容器">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Atuple%E5%AE%B9%E5%99%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>STL_tuple容器 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/myblog/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Atuple%E5%AE%B9%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.png">
      <meta itemprop="name" content="高乾威">
      <meta itemprop="description" content="报我楼成秋望月，把君诗读夜回灯">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          STL_tuple容器
        </h1>

        <div class="post-meta">

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-05-07 21:33:44 / 修改时间：21:54:25" itemprop="dateCreated datePublished" datetime="2021-05-07T21:33:44+08:00">2021-05-07</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/C/" itemprop="url" rel="index"><span itemprop="name">C++</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/C/STL/" itemprop="url" rel="index"><span itemprop="name">STL</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <span class="post-meta-divider">|</span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="一、元组简介"><a href="#一、元组简介" class="headerlink" title="一、元组简介"></a>一、元组简介</h3><p>    tuple是一个固定大小的不同类型值的集合，是泛化的std::pair。我们也可以把他当做一个通用的结构体来用，不需要创建结构体又获取结构体的特征，在某些情况下可以取代结构体使程序更简洁，直观。std::tuple理论上可以有无数个任意类型的成员变量，而std::pair只能是2个成员，因此在需要保存3个及以上的数据时就需要使用tuple元组了。</p>
<span id="more"></span>

<p>    tuple（元组）在c++11中开始引用的。tuple看似简单，其实它是简约而不简单，可以说它是c++11中一个既简单又复杂的东东，关于它简单的一面是它很容易使用，复杂的一面是它内部隐藏了太多细节，要揭开它神秘的面纱时又比较困难。</p>
<p>    #include &lt;tuple&gt;   </p>
<h3 id="二、tuple的创建和初始化"><a href="#二、tuple的创建和初始化" class="headerlink" title="二、tuple的创建和初始化"></a>二、tuple的创建和初始化</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">std::tuple&lt;T1, T2, TN&gt; t1;            </span><br><span class="line"><span class="comment">//创建一个空的tuple对象（使用默认构造），它对应的元素分别是T1和T2...Tn类型，采用值初始化。</span></span><br><span class="line">std::tuple&lt;T1, T2, TN&gt; t2(v1, v2, ... TN);    </span><br><span class="line"><span class="comment">//创建一个tuple对象，它的两个元素分别是T1和T2 ...Tn类型; 要获取元素的值需要通过tuple的成员get&lt;Ith&gt;(obj)进行获取(Ith是指获取在tuple中的第几个元素，请看后面具体实例)。</span></span><br><span class="line"><span class="function">std::tuple&lt;T1&amp;&gt; <span class="title">t3</span><span class="params">(ref&amp;)</span></span>; <span class="comment">// tuple的元素类型可以是一个引用</span></span><br><span class="line">std::<span class="built_in">make_tuple</span>(v1, v2); <span class="comment">// 像pair一样也可以通过make_tuple进行创建一个tuple对象</span></span><br></pre></td></tr></table></figure>


<p>     tuple的元素类型为引用：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">std::string name;</span><br><span class="line">std::tuple&lt;string &amp;, int&gt; tpRef(name, 30);</span><br><span class="line"><span class="comment">// 对tpRef第一个元素赋值，同时name也被赋值 - 引用</span></span><br><span class="line">std::get&lt;<span class="number">0</span>&gt;(tpRef) = <span class="string">&quot;Sven&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// name输出也是Sven</span></span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;name: &quot;</span> &lt;&lt; name &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br></pre></td></tr></table></figure>

<h3 id="三、有关tuple元素的操作"><a href="#三、有关tuple元素的操作" class="headerlink" title="三、有关tuple元素的操作"></a>三、有关tuple元素的操作</h3><h5 id="1-等价结构体"><a href="#1-等价结构体" class="headerlink" title="1.等价结构体"></a>1.等价结构体</h5><p>    开篇讲过在某些时候tuple可以等同于结构体一样使用，这样既方便又快捷。如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">struct person &#123;</span><br><span class="line">    char *m_name;</span><br><span class="line">    char *m_addr;</span><br><span class="line">    int  *m_ages;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;可以用tuple来表示这样的一个结构类型，作用是一样的。</span><br><span class="line">std::tuple&lt;const char *, const char *, int&gt;</span><br></pre></td></tr></table></figure>



<h5 id="2-如何获取tuple元素个数"><a href="#2-如何获取tuple元素个数" class="headerlink" title="2.如何获取tuple元素个数"></a>2.如何获取tuple元素个数</h5><p>    当有一个tuple对象但不知道有多少元素可以通过如下查询：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">std::tuple&lt;int, char, double&gt; mytuple (10, &#x27;a&#x27;, 3.14);</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;mytuple has &quot;</span>;</span><br><span class="line">std::cout &lt;&lt; std::tuple_size&lt;<span class="keyword">decltype</span>(mytuple)&gt;::value;</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot; elements.&quot;</span> &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line"><span class="comment">//输出结果：</span></span><br><span class="line"><span class="comment">//mytuple has 3 elements</span></span><br></pre></td></tr></table></figure>



<h5 id="3-获取元素的值"><a href="#3-获取元素的值" class="headerlink" title="3. 获取元素的值"></a>3. 获取元素的值</h5><p>    获取tuple对象元素的值可以通过get&lt;Ith&gt;(obj)方法进行获取；</p>
<p>    Ith - 是想获取的元素在tuple对象中的位置。</p>
<p>    obj - 是想获取tuple的对象</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">std::tuple&lt;int, char, double&gt; mytuple (10, &#x27;a&#x27;, 3.14);</span><br><span class="line"><span class="comment">//获取元素</span></span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;the elements is: &quot;</span>;</span><br><span class="line">std::cout &lt;&lt; std::get&lt;<span class="number">0</span>&gt;(mytuple) &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">std::cout &lt;&lt; std::get&lt;<span class="number">1</span>&gt;(mytuple) &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">std::cout &lt;&lt; std::get&lt;<span class="number">2</span>&gt;(mytuple) &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line"><span class="comment">//输出结果：</span></span><br><span class="line"><span class="comment">//the elements is: 10 a 3.14 </span></span><br></pre></td></tr></table></figure>


<p>   tuple不支持迭代，只能通过元素索引(或tie解包)进行获取元素的值。但是<strong>给定的索引必须是在编译器就已经给定，不能在运行期进行动态传递，否则将发生编译错误</strong>：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">3</span>; i++)</span><br><span class="line">std::cout &lt;&lt; std::get&lt;i&gt;(mytuple) &lt;&lt; <span class="string">&quot; &quot;</span>; <span class="comment">//将引发编译错误</span></span><br></pre></td></tr></table></figure>



<h5 id="4-获取元素的类型"><a href="#4-获取元素的类型" class="headerlink" title="4.获取元素的类型"></a>4.获取元素的类型</h5><p>     要想得到元素类型可以通过tuple_element方法获取，如有以下元组对象：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">std::tuple&lt;std::string, int&gt; tp(&quot;Sven&quot;, 20);</span><br><span class="line"><span class="comment">// 得到第二个元素类型</span></span><br><span class="line">std::tuple_element&lt;<span class="number">1</span>, <span class="keyword">decltype</span>(tp)&gt;::type ages;  <span class="comment">// ages就为int类型</span></span><br><span class="line">ages = std::get&lt;<span class="number">1</span>&gt;(tp);</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;ages: &quot;</span> &lt;&lt; ages &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line"><span class="comment">//输出结果： </span></span><br><span class="line">ages: <span class="number">20</span></span><br></pre></td></tr></table></figure>



<h5 id="5-利用tie进行解包元素的值"><a href="#5-利用tie进行解包元素的值" class="headerlink" title="5.利用tie进行解包元素的值"></a>5.利用tie进行解包元素的值</h5><p>     如同pair一样也是可以通过tie进行解包tuple的各个元素的值。如下tuple对象有4个元素，通过tie解包将会把这4个元素的值分别赋值给tie提供的4个变量中。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;tuple&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;utility&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **argv)</span> </span>&#123;</span><br><span class="line">    std::tuple&lt;std::string, <span class="keyword">int</span>, std::string, <span class="keyword">int</span>&gt; tp;</span><br><span class="line">    tp = std::<span class="built_in">make_tuple</span>(<span class="string">&quot;Sven&quot;</span>, <span class="number">25</span>, <span class="string">&quot;Shanghai&quot;</span>, <span class="number">21</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义接收变量</span></span><br><span class="line">    std::string name;</span><br><span class="line">    std::string addr;</span><br><span class="line">    <span class="keyword">int</span> ages;</span><br><span class="line">    <span class="keyword">int</span> areaCode;</span><br><span class="line">     </span><br><span class="line">    std::<span class="built_in">tie</span>(name, ages, addr, areaCode) = tp;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;Output: &quot;</span> &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;name: &quot;</span> &lt;&lt; name &lt;&lt;<span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;addr: &quot;</span> &lt;&lt; addr &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;ages: &quot;</span> &lt;&lt; ages &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;areaCode: &quot;</span> &lt;&lt; areaCode &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;     </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*输出结果：</span></span><br><span class="line"><span class="comment">Output: </span></span><br><span class="line"><span class="comment">name: Sven, addr: Shanghai, ages: 25, areaCode: 21</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>


<p>    但有时候tuple包含的多个元素时只需要其中的一个或两个元素，如此可以通过std::ignore进行变量占位，这样将会忽略提取对应的元素。可以修改上述例程：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;tuple&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;utility&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **argv)</span> </span>&#123;</span><br><span class="line">    std::tuple&lt;std::string, <span class="keyword">int</span>, std::string, <span class="keyword">int</span>&gt; tp;</span><br><span class="line">    tp = std::<span class="built_in">make_tuple</span>(<span class="string">&quot;Sven&quot;</span>, <span class="number">25</span>, <span class="string">&quot;Shanghai&quot;</span>, <span class="number">21</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义接收变量</span></span><br><span class="line">    std::string name;</span><br><span class="line">    std::string addr;</span><br><span class="line">    <span class="keyword">int</span> ages;</span><br><span class="line">    <span class="keyword">int</span> areaCode = <span class="number">110</span>;</span><br><span class="line">     </span><br><span class="line">    std::<span class="built_in">tie</span>(name, ages, std::ignore, std::ignore) = tp;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;Output: &quot;</span> &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;name: &quot;</span> &lt;&lt; name &lt;&lt;<span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;addr: &quot;</span> &lt;&lt; addr &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;ages: &quot;</span> &lt;&lt; ages &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;areaCode: &quot;</span> &lt;&lt; areaCode &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">     </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出结果：</span></span><br><span class="line"><span class="comment">Output: </span></span><br><span class="line"><span class="comment">name: Sven, addr: , ages: 25, areaCode: 110</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>


<h5 id="6-tuple元素的引用"><a href="#6-tuple元素的引用" class="headerlink" title="6.tuple元素的引用"></a>6.tuple元素的引用</h5><p>    前面已经列举了将引用作为tuple的元素类型。下面通过引用搭配make_tuple()可以提取tuple的元素值，将某些变量值设给它们，并通过改变这些变量来改变tuple元素的值：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;tuple&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;functional&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **agrv)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    std::tuple&lt;std::string, int, float&gt; tp1(&quot;Sven Cheng&quot;, 77, 66.1);</span><br><span class="line">     </span><br><span class="line">    std::string name;</span><br><span class="line">    <span class="keyword">int</span> weight;</span><br><span class="line">    <span class="keyword">float</span> f;</span><br><span class="line">     </span><br><span class="line">    <span class="keyword">auto</span> tp2 = std::<span class="built_in">make_tuple</span>(std::<span class="built_in">ref</span>(name), std::<span class="built_in">ref</span>(weight), std::<span class="built_in">ref</span>(f)) = tp1;</span><br><span class="line">     </span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;Before change: &quot;</span> &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;name: &quot;</span> &lt;&lt; name &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;weight: &quot;</span> &lt;&lt; weight &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;f: &quot;</span> &lt;&lt; f &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">     </span><br><span class="line">    name = <span class="string">&quot;Sven&quot;</span>;</span><br><span class="line">    weight = <span class="number">80</span>;</span><br><span class="line">    f = <span class="number">3.14</span>;</span><br><span class="line">     </span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;After change: &quot;</span> &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;element 1st: &quot;</span> &lt;&lt; std::get&lt;<span class="number">0</span>&gt;(tp2) &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;element 2nd: &quot;</span> &lt;&lt; std::get&lt;<span class="number">1</span>&gt;(tp2) &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;element 3rd: &quot;</span> &lt;&lt; std::get&lt;<span class="number">2</span>&gt;(tp2) &lt;&lt; <span class="string">&#x27;\n&#x27;</span>;</span><br><span class="line">     </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出结果：</span></span><br><span class="line"><span class="comment">Before change: </span></span><br><span class="line"><span class="comment">name: Sven Cheng, weight: 77, f: 66.1</span></span><br><span class="line"><span class="comment">After change: </span></span><br><span class="line"><span class="comment">element 1st: Sven, element 2nd: 80, element 3rd: 3.14</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/myblog/tags/C/" rel="tag"># C++</a>
              <a href="/myblog/tags/STL/" rel="tag"># STL</a>
              <a href="/myblog/tags/STL%E5%AE%B9%E5%99%A8/" rel="tag"># STL容器</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblog/2021/05/07/STL%EF%BC%9Astring%E5%AE%B9%E5%99%A8/" rel="prev" title="STL_string容器">
      <i class="fa fa-chevron-left"></i> STL_string容器
    </a></div>
      <div class="post-nav-item">
    <a href="/myblog/2021/05/07/STL%EF%BC%9Aunordered_%E7%AD%89%E5%AE%B9%E5%99%A8/" rel="next" title="STL_ map和unordered_map的差别">
      STL_ map和unordered_map的差别 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%85%83%E7%BB%84%E7%AE%80%E4%BB%8B"><span class="nav-text">一、元组简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81tuple%E7%9A%84%E5%88%9B%E5%BB%BA%E5%92%8C%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">二、tuple的创建和初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%9C%89%E5%85%B3tuple%E5%85%83%E7%B4%A0%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">三、有关tuple元素的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-%E7%AD%89%E4%BB%B7%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-text">1.等价结构体</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96tuple%E5%85%83%E7%B4%A0%E4%B8%AA%E6%95%B0"><span class="nav-text">2.如何获取tuple元素个数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-%E8%8E%B7%E5%8F%96%E5%85%83%E7%B4%A0%E7%9A%84%E5%80%BC"><span class="nav-text">3. 获取元素的值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-%E8%8E%B7%E5%8F%96%E5%85%83%E7%B4%A0%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="nav-text">4.获取元素的类型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-%E5%88%A9%E7%94%A8tie%E8%BF%9B%E8%A1%8C%E8%A7%A3%E5%8C%85%E5%85%83%E7%B4%A0%E7%9A%84%E5%80%BC"><span class="nav-text">5.利用tie进行解包元素的值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#6-tuple%E5%85%83%E7%B4%A0%E7%9A%84%E5%BC%95%E7%94%A8"><span class="nav-text">6.tuple元素的引用</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="高乾威"
      src="/myblog/images/avatar.png">
  <p class="site-author-name" itemprop="name">高乾威</p>
  <div class="site-description" itemprop="description">报我楼成秋望月，把君诗读夜回灯</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">59</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="http://github.com/GaoQianwei" title="GitHub → http:&#x2F;&#x2F;github.com&#x2F;GaoQianwei" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2966807184@qq.com" title="E-Mail → mailto:2966807184@qq.com" rel="noopener" target="_blank">E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/xue-wu-zhi-jing-75-22" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;xue-wu-zhi-jing-75-22" rel="noopener" target="_blank">知乎</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45341339" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45341339" rel="noopener" target="_blank">CSDN</a>
      </span>
  </div>



      </div><div>
  <canvas id="canvasDiyBlock" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
<script src="/myblog/js/custom/clock.js"></script>

</div>


    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021-04 到 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">高乾威</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<span class="post-meta-divider">|</span>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 117.2k 字</span>
</div>

<span class="post-meta-divider">|</span>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/myblog/lib/anime.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblog/js/utils.js"></script>

<script src="/myblog/js/motion.js"></script>


<script src="/myblog/js/schemes/pisces.js"></script>


<script src="/myblog/js/next-boot.js"></script>


  <script defer src="/myblog/lib/three/three.min.js"></script>
    <script defer src="/myblog/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/myblog/js/local-search.js"></script>













  

  

</body>
</html>
